/**
 * @date   2011-5-10
 * @author tianye
 */
package cn.edu.dlut.WingGame.GUI;

import android.view.MotionEvent;
import cn.edu.dlut.WingGame.BaseType.ObjectState;
import cn.edu.dlut.WingGame.BaseType.WingColor;
import cn.edu.dlut.WingGame.Graphic.GLFont;
import cn.edu.dlut.WingGame.IOsystem.TouchListener;
import cn.edu.dlut.WingGame.IOsystem.WingTouch;
import cn.edu.dlut.WingGame.Object.WingObject;

public class WingTextButton extends WingObject implements TouchListener {
	protected boolean oldFlag = false;
	protected int delay = 0;
	protected boolean isActive = false;
	protected float x = 0;
	protected float y = 0;
	protected float width = 100;
	protected float height = 30;
	protected GLFont normalFont;
	protected GLFont activeFont;
	protected String normalText = "Button";
	protected String activeText = "Button";

	public WingTextButton() {
		WingTouch.addTouchListener(this);
	}

	public WingTextButton(String name) {
		super(name);
		WingTouch.addTouchListener(this);
	}

	public void setButtonState(boolean flag) {
		isActive = flag;
	}

	public boolean getButtonState() {
		boolean tmp = isActive;
		if (isActive)
			oldFlag = false;
		isActive = false;
		return tmp;
	}

	public void set(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public void set(float x, float y, float width, float height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}

	public void setNormalFont(String fontname, int fontsize,
			WingColor fontcolor, boolean isbold, boolean isitalic) {
		normalFont = new GLFont();
		normalFont.creatFont(fontname, fontsize, fontcolor, isbold, isitalic);
	}

	public void setNormalFont(int fontsize, WingColor fontcolor) {
		normalFont = new GLFont();
		normalFont.creatFont(fontsize, fontcolor);
	}

	public void setNormalFont(String ttfPath, int fontsize, WingColor fontcolor) {
		normalFont = new GLFont();
		normalFont.creatFont(fontsize, fontcolor);
	}

	public void setActiveFont(String fontname, int fontsize,
			WingColor fontcolor, boolean isbold, boolean isitalic) {
		activeFont = new GLFont();
		activeFont.creatFont(fontname, fontsize, fontcolor, isbold, isitalic);
	}

	public void setActiveFont(int fontsize, WingColor fontcolor) {
		activeFont = new GLFont();
		activeFont.creatFont(fontsize, fontcolor);
	}

	public void setActiveFont(String ttfPath, int fontsize, WingColor fontcolor) {
		activeFont = new GLFont();
		activeFont.creatFont(fontsize, fontcolor);
	}

	public void setText(String text) {
		normalText = text;
		activeText = text;
	}

	public void setNormalText(String text) {
		normalText = text;
	}

	public void setActiveText(String text) {
		activeText = text;
	}

	@Override
	public void render() {
		if (delay == 2) {
			isActive = oldFlag;
			delay++;
		} else if (delay < 2)
			delay++;

		if (oldFlag)
			normalFont.drawText(x, y, activeText);
		else
			activeFont.drawText(x, y, normalText);
	}

	@Override
	public void update() {

		if (mState == ObjectState.died)
			WingTouch.delTouchListener(mID);

	}

	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if ((event.getX() > x) && (event.getX() < x + width)
					&& (event.getY() > y) && (event.getY() < y + height)) {
				oldFlag = true;
				delay = 0;
			}
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			oldFlag = false;
			delay = 0;
		}
		return false;
	}
}
